package hc_flash_base3.util.performance
{
	import flash.display.*;
	import flash.text.*
	import flash.events.*;
	import flash.utils.Timer;
	
	/** Simple FPS Counter. Can be added to displayObjectContainer */
	
	class fpsCounter extends Sprite
	{
		private var fpsTextField:TextField=null;
				
		private var nextMeasurementCountDown:int;
		private var lastTimerMeasurement:int;
		private var frameCountInterval:int;
				
		private var fpsTextFormat:TextFormat;
		
		static private var fpsHeight:int=23;
		static private var fpsWidth:int=23;
				
		function fpsCounter(_frameCountInterval:int = 10)
		{
			frameCountInterval=_frameCountInterval;
			
			fpsTextFormat=new TextFormat("_sans",11,0x000000,true);
			fpsTextFormat.align="center";
			// Create a textfield
			
			fpsTextField=new TextField();			
			fpsTextField.defaultTextFormat=fpsTextFormat;
						
            	fpsTextField.width = fpsWidth;
	            fpsTextField.height = fpsHeight;
	            fpsTextField.background = true;
	            fpsTextField.border = false;
			fpsTextField.selectable=false;			
			fpsTextField.text="FPS"
			
			// Add the textfield to the display list
			addChild(fpsTextField);
			
			nextMeasurementCountDown=frameCountInterval;
			lastTimerMeasurement=flash.utils.getTimer();
			this.addEventListener(Event.ENTER_FRAME,update);
		}
		
		function update(_event:Event ):void
		{			
			nextMeasurementCountDown--;
			if (!nextMeasurementCountDown) {
				nextMeasurementCountDown=frameCountInterval;
				var oldTimerValue:int=lastTimerMeasurement;
				lastTimerMeasurement=flash.utils.getTimer();				
				var fps:Number=frameCountInterval*1000/(lastTimerMeasurement-oldTimerValue);
				var displayValue:String=fps;
				displayValue=displayValue.split(".")[0];
				fpsTextField.text=displayValue;				
			}
		}
	}
}